package com.xyht.medicine_management_system.modules.aboutUs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.medicine_management_system.common.commonReturn.CommonCodeEnum;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.modules.aboutUs.entity.MmsAboutUs;
import com.xyht.medicine_management_system.modules.aboutUs.entity.req.MmsAboutUsReq;
import com.xyht.medicine_management_system.modules.aboutUs.entity.resp.MmsAboutUsResp;
import com.xyht.medicine_management_system.modules.aboutUs.mapper.MmsAboutUsMapper;
import com.xyht.medicine_management_system.modules.aboutUs.service.MmsAboutUsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 关于我们 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-02-19
 */
@Service
public class MmsAboutUsServiceImpl extends ServiceImpl<MmsAboutUsMapper, MmsAboutUs> implements MmsAboutUsService {

    @Resource
    private MmsAboutUsMapper mmsAboutUsMapper;

    @Override
    public ResponseResult addAboutUs(String userId, MmsAboutUsReq addReq) {
        if (isNullOrEmpty(addReq.getCompanyProfile(),addReq.getQualificationInformation(),addReq.getContactTel())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        MmsAboutUs addAboutUs = new MmsAboutUs();
        BeanUtils.copyProperties(addReq,addAboutUs);
        addAboutUs.setUserId(userId);
        mmsAboutUsMapper.insert(addAboutUs);
        return CommonResult.success();
    }

    @Override
    public ResponseResult updateAboutUs(MmsAboutUsReq updateReq) {
        if (isNullOrEmpty(updateReq.getId(),updateReq.getCompanyProfile(),updateReq.getQualificationInformation(),updateReq.getContactTel())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        MmsAboutUs updateAboutUs = mmsAboutUsMapper.selectById(updateReq.getId());
        if (isNullOrEmpty(updateAboutUs)) {
            return CommonResult.failed(CommonCodeEnum.ABOUT_US_NOT_EXIST);
        }
        BeanUtils.copyProperties(updateReq,updateAboutUs);
        //存记录
        MmsAboutUs addAboutUs = new MmsAboutUs();
        addAboutUs.setUserId(updateAboutUs.getUserId());
        BeanUtils.copyProperties(updateAboutUs,addAboutUs,"id");
        mmsAboutUsMapper.insert(addAboutUs);

        return CommonResult.success();
    }

    @Override
    public ResponseResult getAboutUsDetail() {
        MmsAboutUsResp mmsAboutUsResp = new MmsAboutUsResp();
        List<MmsAboutUs> aboutUsList = mmsAboutUsMapper.selectList(new LambdaQueryWrapper<MmsAboutUs>()
                .orderByDesc(MmsAboutUs::getUpdateTime));
        //获取最新的一条数据
        if (!aboutUsList.isEmpty()) {
            MmsAboutUs mmsAboutUs = aboutUsList.get(0);
            BeanUtils.copyProperties(mmsAboutUs,mmsAboutUsResp);
        }
        return CommonResult.success(mmsAboutUsResp);
    }

    @Override
    public ResponseResult getAboutUsList(Integer pageNum, Integer pageSize, String search) {
        Page<MmsAboutUs> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<MmsAboutUs> lambdaQueryWrapper = new LambdaQueryWrapper<MmsAboutUs>().orderByDesc(MmsAboutUs::getUpdateTime);
        if (!isNullOrEmpty(search)) {
            lambdaQueryWrapper.like(MmsAboutUs::getCompanyProfile,search);
        }
        mmsAboutUsMapper.selectPage(page,lambdaQueryWrapper);
        List<MmsAboutUsResp> aboutUsList = page.getRecords()
                .stream()
                .map(mmsAboutUs -> {
                    MmsAboutUsResp mmsAboutUsResp = new MmsAboutUsResp();
                    BeanUtils.copyProperties(mmsAboutUs,mmsAboutUsResp);
                    return mmsAboutUsResp;
                })
                .collect(Collectors.toList());
        return CommonResult.success(aboutUsList,(int)page.getTotal());
    }
}
